package com.zhixiao.user.util.impl;

import com.zhixiao.user.util.IdGenerator;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
@Primary
public class SnowflakeIdGenerator implements IdGenerator {
    @Value("${snowflake.datacenter:1}")
    private long datacenterId;

    @Value("${snowflake.worker:1}")
    private long workerId;

    private SnowflakeIdWorker idWorker;

    @PostConstruct
    public void init() {
        idWorker = new SnowflakeIdWorker(workerId, datacenterId);
    }

    @Override
    public String generateId() {
        return String.valueOf(idWorker.nextId());
    }
}